package com.youme.section.login.sms

import androidx.lifecycle.observe
import com.salton123.ui.base.YoumeBaseFragment
import com.hjq.toast.ToastUtils
import com.hyphenate.easeim.BuildConfig
import com.hyphenate.easeim.DemoHelper
import com.hyphenate.easeim.R
import com.hyphenate.easeim.common.enums.Status
import com.hyphenate.easeim.common.repositories.EMClientRepository
import com.youme.RouterManager
import com.youme.viewmodel.YouMeApiViewModel
import com.youme.widget.CountDownTextView
import kotlinx.android.synthetic.main.youme_fragment_register_step_two.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @Time:2021/3/26 17:06
 * @Author:wujinsheng
 * @Description:
 */
class YouMeSmsLoginStepTwoFragment(var phoneNumber: String, var viewModel: YouMeApiViewModel) : YoumeBaseFragment() {
    override fun getLayoutId(): Int = R.layout.youme_fragment_register_step_two

    var verifyCode = ""
        get() = etCode.text.toString()

    override fun initViewAndData() {
        btnLogin.setOnClickListener {
            GlobalScope.launch {
                viewModel.mobileLogin(verifyCode, phoneNumber).getOrNull()?.apply {
                    if (isNotEmpty()) {
                        withContext(Dispatchers.Main) {
                            EMClientRepository().loginToServer(phoneNumber, "${phoneNumber}_youme", false)
                                .observe(viewLifecycleOwner) {
                                    if (it.status == Status.SUCCESS) {
                                        loginSuccess()
                                    } else if (it.status == Status.ERROR) {
                                        ToastUtils.show("登录失败:${it.message}")
                                    }
                                }
                        }
                    } else {
                        withContext(Dispatchers.Main) {
                            ToastUtils.show("登录失败")
                        }
                    }
                }
            }
        }
        setCountdownSetting()
        if (BuildConfig.APP_DEVELOP) {
            etCode.setText("9999")
        }
    }

    private fun loginSuccess() {
        DemoHelper.getInstance().autoLogin = true
        ToastUtils.show("登录成功")
        activity?.let { it1 -> RouterManager.main(it1) }
    }

    private fun setCountdownSetting() {
        tvRemind.setNormalText(getString(R.string.homepage_verify_get_sms_code))
            .setCountDownText(getString(R.string.homepage_verify_get_sms_again), ")")
            .setCloseKeepCountDown(false)
            .setCountDownClickable(false)
            .setShowFormatTime(true)
            .isSupportBlankMobileSend(false)
            .setOnCountDownFinishListener(object : CountDownTextView.OnCountDownFinishListener {
                override fun onFinish() {
                }
            })
        etCode.isFocusable = true
        etCode.requestFocus()
        tvRemind.startCountDown(60)
    }
}